package openbang.account.web.portal.widget;

import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;

/**
 * 邮件地址的文本框
 * @author aibo zeng
 *
 */

public class EmailTextBox extends TextBox{
   public int emailNum = 1;//允许录入多少个邮件地址,0代表不限。最多？至少？必须多少个？  
   public static String separator = ";";
   /**
    * 正则表达式 
    */
   public static String regex = "^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_-])+((\\.[a-zA-Z0-9_-]{2,3}){1,2})";
   //Pattern pattern = Pattern.compile(regex);
   public EmailTextBox(){
	   super();
   }
   public EmailTextBox(int emailNum){
	   super();
	   this.emailNum = emailNum;
   }
   public EmailTextBox(Element element){
	   super(element);	   
   }
   public static EmailTextBox wrap(Element element) {
	    // Assert that the element is attached.
	    assert Document.get().getBody().isOrHasChild(element);
	    EmailTextBox textBox = new EmailTextBox(element);
	    // Mark it attached and remember it for cleanup.
	    textBox.onAttach();
	    RootPanel.detachOnWindowClose(textBox);
	    return textBox;
   }   
   
   public String[] getArrValue(){
	   String[] receStrArr=null;
	   if(getText()==null || getText().trim().length()<=0){
		   return new String[]{""};
	   }
	   if(emailNum==1 || getText().indexOf(separator)<0){
			receStrArr = new String[1];
			receStrArr[0]= getText();
			return receStrArr;
	   }
  	   receStrArr = getText().split(";");
	   return receStrArr;
   }
   /**
    * 判断录入是否有效     
    * @return 出错的第几个   0不出错 , 1代表第一个,...
    */
   public int invalidNum(){
	   String[]  addr = getArrValue();
	   if(addr==null){
		   return 1;
	   }
	   for(int i=0;i<addr.length;i++){
		   //pattern.matcher(addr[i]).matches()
		   if(addr[i]==null || !addr[i].matches(regex)){
			   return i+1;
		   }
	   }
	   return 0;
   }
}
